package com.example.tool.activtiy.recylerview;

import android.graphics.Color;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.tool.R;
import com.example.tool.activtiy.base.BaseActivity;
import com.example.tool.adapter.MultiAdapter;
import com.example.tool.adapter.MultipleItem;
import com.example.tool.adapter.base.decoration.RecycleViewDivider;
import com.example.tool.mvp.CommonPresent;
import com.example.tool.util.ToastUtil;
import com.example.tool.widget.CustomLoadMoreView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;

/**
 * Created Date 2019/8/25.
 *
 * @author liangkun
 * ClassDescription：多条目
 */
public class MultiItemRvActivity extends BaseActivity<CommonPresent> {
    @BindView(R.id.rv_multi)
    protected RecyclerView rvMulti;
    @BindView(R.id.tv_title)
    protected TextView tvTitle;
    @BindView(R.id.btn_reset)
    protected FloatingActionButton fbBtn;
    @BindView(R.id.swipeLayout)
    protected SwipeRefreshLayout refreshLayout;
    private MultiAdapter multiAdapter;
    private List<MultipleItem> datas;
    private View notDataView;
    private View errorView;

    @Override
    protected int getLayoutId() {
        return R.layout.activtiy_multi_rv;
    }

    @Override
    protected void initView() {
        tvTitle.setText("多条目view");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rvMulti.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL,
                getResources().getDimensionPixelOffset(R.dimen.sw_dp_15), 0, 0, getResources().getColor(R.color.color_white)));
        rvMulti.setLayoutManager(linearLayoutManager);
        rvMulti.setLayoutManager(new LinearLayoutManager(this));
        datas = getMultipleItemData();
        multiAdapter = new MultiAdapter(new ArrayList<MultipleItem>());
        //设置下拉刷新的颜色
        refreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
//        refreshLayout.setRefreshing(true);
        //设置加载更多
        multiAdapter.setLoadMoreView(new CustomLoadMoreView());
        multiAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        //无数据

        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) rvMulti.getParent(), false);
        notDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });

        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rvMulti.getParent(), false);
        errorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefreshTwo();
            }
        });
        rvMulti.setAdapter(multiAdapter);
        onRefresh();
    }

    private void onRefresh() {

        multiAdapter.setEmptyView(R.layout.loading_view, (ViewGroup) rvMulti.getParent());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                multiAdapter.setEmptyView(errorView);

            }
        }, 2000);
    }

    private void onRefreshTwo() {
        getMultipleItemData();
        multiAdapter.setEmptyView(R.layout.loading_view, (ViewGroup) rvMulti.getParent());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                multiAdapter.setNewData(getMultipleItemData());

            }
        }, 2000);
    }


    public static List<MultipleItem> getMultipleItemData() {
        List<MultipleItem> list = new ArrayList<>();
        for (int i = 0; i <= 30; i++) {
            list.add(new MultipleItem(new Random().nextInt(3)+1, MultipleItem.IMG_SPAN_SIZE));
            list.add(new MultipleItem(new Random().nextInt(3)+1, MultipleItem.TEXT_SPAN_SIZE, "热血江湖"));
            list.add(new MultipleItem(new Random().nextInt(3)+1, MultipleItem.IMG_TEXT_SPAN_SIZE));
//            list.add(new MultipleItem(MultipleItem.IMG_TEXT, MultipleItem.IMG_TEXT_SPAN_SIZE_MIN));
//            list.add(new MultipleItem(MultipleItem.IMG_TEXT, MultipleItem.IMG_TEXT_SPAN_SIZE_MIN));
        }

        return list;
    }

    @Override
    protected void initData() {
        multiAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {

            }
        });

        multiAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                ToastUtil.toastShortContent("加载更多的数据");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        multiAdapter.loadMoreFail();
//
//                        new Handler().postDelayed(new Runnable() {
//                            @Override
//                            public void run() {
//                                multiAdapter.loadMoreComplete();
////                                new Handler().postDelayed(new Runnable() {
////                                    @Override
////                                    public void run() {
////                                        multiAdapter.loadMoreEnd();
////
////
////                                    }
////                                }, 2000);
//
//                            }
//                        }, 2000);


                    }
                }, 2000);

            }
        });
    }

    @Override
    protected void initEvent() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        fbBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.toastShortContent("拉取数据---");
            }
        });
    }

    private void refresh() {
        ToastUtil.toastShortContent("下拉刷新");
        multiAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        //成功后，设置可以加载更多
//        multiAdapter.setEnableLoadMore(true);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                multiAdapter.setEnableLoadMore(true);
                refreshLayout.setRefreshing(false);
            }
        }, 2000);

    }

    @Override
    protected CommonPresent createPresenter() {
        return null;
    }


}
